package com.sina.common.util;

import android.util.Log;

import com.sina.common.BuildConfig;


/**
 * Log 工具
 */
public class LogUtils {

    private static boolean mShowLog = BuildConfig.show_log;

    private static String mTag = "WithWings";

    /**
     * 得到一个信息的详细的情况【类名+方法名+行号】
     *
     * @param message 要显示的信息
     * @return 一个信息的详细的情况【类名+方法名+行号+message】
     */
    private static String getDetailMessage(String message) {
        String detailMessage = "";
        for (StackTraceElement ste : (new Throwable()).getStackTrace()) {
            //栈顶肯定是LogUtil这个类自己
            if (!LogUtils.class.getName().equals(ste.getClassName())) {
                //栈顶的下一个就是需要调用这个类的地方[此处取出类名和方法名还有行号]
                int b = ste.getClassName().lastIndexOf(".") + 1;
                String TAG = ste.getClassName().substring(b);
                detailMessage = TAG + "->" + ste.getMethodName() + "():" + ste.getLineNumber()
                        + "->" + message;
                break;
            }
        }
        return detailMessage;
    }

    /**
     * 简单级别
     * @param message 信息内容
     */
    public static void v(String message) {
        if (mShowLog) {
            Log.v(mTag, getDetailMessage(message));
        }
    }

    /**
     * debug级别
     * @param message 信息内容
     */
    public static void d(String message) {
        if (mShowLog) {
            Log.d(mTag, getDetailMessage(message));
        }
    }

    /**
     * 普通级别
     * @param message 信息内容
     */
    public static void i(String message) {
        if (mShowLog) {
            Log.i(mTag, getDetailMessage(message));
        }
    }

    /**
     * 警告级别
     * @param message 信息内容
     */
    public static void w(String message) {
        if (mShowLog) {
            Log.w(mTag, getDetailMessage(message));
        }
    }

    /**
     * 错误级别
     * @param message 信息内容
     */
    public static void e(String message) {
        if (mShowLog) {
            Log.e(mTag, getDetailMessage(message));
        }
    }

    public static void logCatchedException(Throwable e) {
        if (!mShowLog || null == e) {
            return;
        }
        StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
        logExceptionInternal(caller, e, Integer.MAX_VALUE);
    }

    public static void logCatchedException(Throwable e, int logLineCount) {
        if (!mShowLog || null == e) {
            return;
        }
        StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
        logExceptionInternal(caller, e, logLineCount);
    }

    private static void logExceptionInternal(StackTraceElement caller, Throwable e, int logLineCount) {
        String TAG = generateTag(caller);
        loge(TAG, "", e.getMessage());
        StackTraceElement[] es = e.getStackTrace();
        int finalLogLineCount = Math.min(logLineCount, es.length);
        for (int i = 0; i < finalLogLineCount; i++) {
            loge(TAG, "", es[i].toString());
        }
    }

    private static String generateTag(StackTraceElement stack) {
        String tag = "%s(L:%d)";
        String className = stack.getClassName();
        className = className.substring(className.lastIndexOf(".") + 1);
        tag = String.format(tag, className, stack.getLineNumber());
        return tag;
    }

    private static void logd(String tag, String methodName, String message) {
        Log.d(tag, methodName + message);
    }

    private static void logd(String tag, String methodName, String message, Throwable throwable) {
        Log.d(tag, methodName + message, throwable);
    }

    private static void loge(String tag, String methodName, String message) {
        Log.e(tag, methodName + message);
    }

    private static void loge(String tag, String methodName, String message, Throwable throwable) {
        Log.e(tag, methodName + message, throwable);
    }

}
